home *** CD-ROM | disk | FTP | other *** search
- /*
- * Turbo C demonstration program FUNCPTRS.C 06/12/87
- *
- * This program demonstrates setting up an ARRAY of POINTERS to FUNCTIONS.
- *
- * int (*FNum[4])() is an array of 4 pointers to functions and is initialized
- * with the 4 elements pointing to 4 different functions.
- *
- * Functions F1 .. F4 are never called directly. Instead, they are called
- * using with the statement:
- *
- * (*FNum[x])();
- *
- * where "x" is an index into the array thus selecting the desired function.
- *
- * by David W. Terry 71560,3550
- */
-
- void F1();
- void F2(); /* function prototypes - necessary for array initialization */
- void F3();
- void F4();
-
- main() {
- int (*FNum[4])() = {F1,F2,F3,F4}; /* initialize function pointers */
- char Ch;
-
- printf("This program demonstrates setting up an ARRAY of POINTERS to FUNCTIONS.");
-
- do {
- printf("\n\nType 1,2,3,4 or Q: ");
- Ch=getch();
- printf("%c\n\n",Ch);
- if (Ch >= '1' && Ch <= '4')
- (*FNum[Ch-'1'])(); /* this one line will call all 4 funtions */
- } while (toupper(Ch) != 'Q');
- }
-
- void F1() {
- printf("You're in Function 1");
- }
-
- void F2() {
- printf("You're in Function 2");
- }
-
- void F3() {
- printf("You're in Function 3");
- }
-
- void F4() {
- printf("You're in Function 4");
- }